package com.xw.dao;

import com.xw.entity.News;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Description: NewsDao
 * @Author: ASUS
 * @Date: 2024/12/19 9:17
 * @Version: 1.0
 */
@Mapper
public interface NewsDao {

    // 新增新闻
    @Insert("INSERT INTO news (title, content, create_time, update_time) VALUES (#{title}, #{content}, NOW(), NOW())")
    int addNews(News news);

    // 删除新闻
    @Delete("DELETE FROM news WHERE id = #{id}")
    int deleteNews(Integer id);

    // 更新新闻
    @Update("UPDATE news SET title = #{title}, content = #{content}, update_time = NOW() WHERE id = #{id}")
    int updateNews(News news);

    // 根据ID查询新闻
    @Select("SELECT id, title, content, create_time, update_time FROM news WHERE id = #{id}")
    News getNewsById(Integer id);

    // 查询所有新闻 倒序
    @Select("SELECT id, title, content, create_time, update_time FROM news ORDER BY create_time DESC")
    List<News> getAllNews();

    //    default List<News> getAllNews() {
//        logger.info("Executing getAllNews query");
//        return null;
//    };
    // 根据关键字查询新闻
    @Select("SELECT id, title, content, create_time, update_time FROM news WHERE title LIKE CONCAT('%', #{title}, '%') OR content LIKE CONCAT('%', #{content}, '%')")
    List<News> searchNewsByKeywords(String keywords);
}
